package com.example.woke;

import com.zhongcai.online.R;
import com.woke.fragment.Myloan1Fragment;
import com.woke.fragment.Myloan2Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;

public class MyloanActivity extends FragmentActivity implements OnCheckedChangeListener {
	
	private FragmentTabHost mTabHost;
	private RadioGroup radioGroup;
	private RelativeLayout mLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myloan);
		
		initTab();
		
	}
	
	private void initTab() {
		findViewById(R.id.avmyloan_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mLayout = (RelativeLayout) findViewById(R.id.avlayout_bottom);
		mTabHost.setHorizontalScrollBarEnabled(false);
		mTabHost.setVerticalScrollBarEnabled(false);
		mTabHost.setup(MyloanActivity.this, getSupportFragmentManager(),
				R.id.avrealtabcontent);
		mTabHost.getTabWidget().setVisibility(View.GONE);

		mTabHost.addTab(mTabHost.newTabSpec("avindex").setIndicator("avHome"),
				Myloan1Fragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("avmonitor").setIndicator("avMessage"),
				Myloan2Fragment.class, null);
		mTabHost.setCurrentTabByTag("avindex");
		((RadioButton) findViewById(R.id.avradio_index)).setChecked(true);
		radioGroup = (RadioGroup) findViewById(R.id.avradiogroup);
		radioGroup.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		FragmentManager fm = getSupportFragmentManager();
		Myloan1Fragment avindex = (Myloan1Fragment) fm.findFragmentByTag("avindex");
		Myloan2Fragment avmonitor = (Myloan2Fragment) fm.findFragmentByTag("avmonitor");
		FragmentTransaction ft = fm.beginTransaction();
		// ** Detaches the androidfragment if exists */
		if (avindex != null)
			ft.detach(avindex);
		if (avmonitor != null)
			ft.detach(avmonitor);
		switch (checkedId) {
		case R.id.avradio_index:
			if (avindex == null) {
				ft.add(R.id.avrealtabcontent, new Myloan1Fragment(), "avindex");
			} else {
				ft.attach(avindex);
			}
			mTabHost.setCurrentTabByTag("avindex");
			break;
		case R.id.avradio_monotor:
			if (avmonitor == null) {
				ft.add(R.id.avrealtabcontent, new Myloan2Fragment(), "avmonitor");
			} else {
				ft.attach(avmonitor);
			}
			mTabHost.setCurrentTabByTag("avmonitor");
			break;
		}
	}
	
}
